package com.banyuan.lesson.集合.Colleection.练习;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 1.随机生成20条狗
 * 颜色随机:白色、黄色、黑色、黄白、黑白
 * 体重随机:[1-50]斤
 * 2.将这些狗依次放入ArrayList中
 * 3.将该list倒序
 * 4.计算list中这些狗的平均体重
 * 5.将体重大于平均值的并且颜色为白色的狗从list中给删除
 * 6.判断该list是否包含体重为20，颜色是白色的狗狗
 * 7.如果存在，则打印该狗狗的下标
 * 8.找到体重最小和最大的狗狗，交换他们的位置
 * 9.对该list按照狗狗的体重降序
 */
public class DogList {
    public static void main(String[] args) {
        List<Dog> list = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            list.add(new Dog());
        }

        System.out.println(list);
        Collections.reverse(list);

        //计算list中这些狗的平均体重；
        double sumWeight = 0;
        for (Dog dog : list) {
            sumWeight += dog.getWeight();
        }
        double averageWeight = sumWeight / list.size();

        //将体重大于平均值的并且颜色为白色的狗从list中给删除
        for (Dog dog : list) {
            if (dog.getColor().equals("白色") && dog.getWeight() > averageWeight) {
                list.remove(dog);
            }
        }

        //判断该list是否包含体重为20，颜色是白色的狗狗
        Dog d1 = new Dog("白色", 20);
        List<Integer> d1List = new ArrayList<>();
        int i = 0;
        for (Dog dog : list) {
            if (dog.equals(d1)) {
                System.out.println(i);
                d1List.add(i);
            }
            i++;
        }

        //找到体重最小和最大的狗交换它们的位置；
        int maxWeight = list.get(0).getWeight();
        int minWeight = list.get(0).getWeight();
        int maxWeightIndex = 0;
        int minWeightIndex = 0;
        int i1 = 0;
        for (Dog dog : list) {
            if (maxWeight < dog.getWeight()) {
                maxWeight = dog.getWeight();
                maxWeightIndex = i;
            }
            if (minWeight > dog.getWeight()) {
                minWeight = dog.getWeight();
                minWeightIndex = i;
            }
            i++;

            Dog temp = list.get(maxWeightIndex);
            list.remove(maxWeight);
            list.add(maxWeightIndex,list.get(maxWeightIndex));
            list.remove(minWeightIndex);
            list.add(minWeightIndex,temp);
        }

        //可以利用Collections中的swap方法,交换两个下标中的元素；
        //Collections.swap(list, minWeightIndex, maxWeightIndex);

        //对该list按照狗狗的体重降序
        list.sort(Comparator.comparing(Dog::getWeight).reversed());
    }
}
